﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Model;
using System.Data;

using Jzy.Model.Mail;
using Frame.Lib.Core.Base;
using Frame.Lib.Data;

namespace Jzy.DAL.Mail
{
    public class WebMailAccountDAL:BaseDAL
    {
        private int _userID;
        public WebMailAccountDAL(int userID)
        {
            _userID = userID;
        }

        public List<WebMailAccountInfo> GetList()
        {
            string sql = "select * from " + OTableName.WebMailAccount + " where UserID = @p0";
            List<WebMailAccountInfo> list = dbOper.Query<WebMailAccountInfo>(sql, _userID);
            return list;
        }

        public void ClearDefault()
        {
            string sql = "update " + OTableName.WebMailAccount + " set IsDefault=0 where UserID = " + _userID;
            dbOper.RunSql(sql);
        }

        public int GetDefaultID()
        {
            string sql = "select top 1 ID from " + OTableName.WebMailAccount + " where UserID = " + _userID + " and IsDefault=1 " +
                        "union select top 1 ID from " + OTableName.WebMailAccount + " where UserID = " + _userID;
            int id = 0;
            DataTable dt = dbOper.Query(sql);
            if (dt.Rows.Count > 0)
            {
                id = (int)dt.Rows[0][0];
            }
            return id;
        }

        public int GetIDByAddr(string mailAddr)
        {
            string strSql = "select ID from " + OTableName.WebMailAccount + " where MailAddr='" + mailAddr + "'";
            int id = dbOper.GetScalar<int>(strSql);
            return id;
        }

        public static List<WebMailAccountInfo> GetAllList()
        {
            string sql = "select * from " + OTableName.WebMailAccount;
            List<WebMailAccountInfo> list = DBHelper.Instance().Query<WebMailAccountInfo>(sql);
            return list;
        }
    }
}
